public class MyLinkedList {


        static class ListNode{
            int val;
            ListNode next;
            public ListNode(int val){
                this.val = val;
            }
        }
        public ListNode head;

        public MyLinkedList() {
            this.head = null;
        }

        public int get(int index) {
            if(index<0){
                return -1;
            }
            ListNode cur = this.head;
            while(cur!=null&&index-1>0){
                cur = cur.next;
                index--;
            }
            if(index-1>0){
                return -1;
            }else{
                return cur.val;
            }
        }

        public void addAtHead(int val) {
            ListNode cur = new ListNode(val);
            cur.next = head;
            this.head = cur;
        }

        public void addAtTail(int val) {
            if(this.head == null){
                this.head = new ListNode(val);
                return ;
            }
            ListNode cur = head;
            while(cur.next!=null){
                cur = cur.next;
            }
            ListNode newnode = new ListNode(val);
            cur.next = newnode;
        }

        public void addAtIndex(int index, int val) {
            if(index<=1){
                addAtHead(val);
                return ;
            }
            ListNode cur = this.head;
            while(cur.next!=null&&index-1>0){
                cur = cur.next;
                index--;
            }
            if(index-1>0){
                return;
            }else{
                ListNode newnode = new ListNode(val);
                newnode.next = cur.next;
                cur.next = newnode;
            }

        }

        public void deleteAtIndex(int index) {
            if(index<=0){
                return ;
            }
            if(index==1){
                this.head = this.head.next;
                return ;
            }
            ListNode cur = this.head;
            while(cur.next!=null&&index-1>0){
                index--;
                cur = cur.next;
            }
            if(index-1>0){
                return ;
            }else{
                cur.next = cur.next.next;
            }
        }

    public static void main(String[] args) {
        MyLinkedList obj = new MyLinkedList();
        obj.addAtHead(1);
        obj.addAtTail(3);
        obj.addAtIndex(1,2);
        obj.get(1);
        obj.deleteAtIndex(1);
        obj.get(1);
    }
}
